package com.android.lovepick;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.android.lovepick.util.BaseUtils;

public class TestRecOrderActivity extends Activity
{
	private static final String TEST_REC_ORDER_TAG = "TestRecOrderActivity";
	private final static int JSON_REC_PARSE_SUCCESS = 0;
	private final static int JSON_REC_PARSE_FAIL = -1;
	private static ListView lvTestRecOrder;
	private static SimpleAdapter mAdapter = null;
	private static String[] from = new String[] { "src", "dest", "goTime",
			"orderID", "money", "state" };
	private static int[] to = new int[] { R.id.myorder_list_src,
			R.id.myorder_list_dest, R.id.myorder_list_gotime,
			R.id.myorder_list_orderid, R.id.myorder_list_money,
			R.id.myorder_list_state };

	private String jsonString = null;
	private static ProgressDialog mDialog = null;
	private static List<Map<String, String>> list = null;
	private TestRecOrderHandler mHandler;

	@Override
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.test_recom_order);
		jsonString = this.getIntent().getStringExtra("recom_order");
		lvTestRecOrder = (ListView) this.findViewById(R.id.lv_recom_order);
		mHandler = new TestRecOrderHandler(this);
		mDialog = new ProgressDialog(this);
		mDialog.setTitle("解析推荐路线数据");
		mDialog.setMessage("正在解析推荐路线数据, 请稍候...");
		mDialog.show();

		new Thread(new Runnable()
		{

			public void run()
			{
				// TODO Auto-generated method stub
				Log.i(TEST_REC_ORDER_TAG, "parse rec_order json...");
				list = BaseUtils.parseJsonFromString(jsonString);
				if (null == list)
				{
					Message msg = new Message();
					msg.what = JSON_REC_PARSE_FAIL;
					mHandler.sendMessage(msg);
				}
				else
				{
					Message msg = new Message();
					msg.what = JSON_REC_PARSE_SUCCESS;
					mHandler.sendMessage(msg);
				}
			}
		}).start();
	}

	private static class TestRecOrderHandler extends Handler
	{
		private final String MYORDER_HANDLER_TAG = "TestRecOrderHandler";
		private final WeakReference<TestRecOrderActivity> mReference;

		public TestRecOrderHandler(TestRecOrderActivity activity)
		{
			mReference = new WeakReference<TestRecOrderActivity>(activity);
		}

		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case JSON_REC_PARSE_SUCCESS:
					Log.i(MYORDER_HANDLER_TAG, "json parse successful");
					mDialog.dismiss();
					mAdapter = new SimpleAdapter(mReference.get(), list,
							R.layout.test_recom_order_list, from, to);
					lvTestRecOrder.setAdapter(mAdapter);
					lvTestRecOrder
							.setOnItemClickListener(new AdapterView.OnItemClickListener()
							{

								public void onItemClick(AdapterView<?> arg0,
										View arg1, int arg2, long arg3)
								{
									// TODO Auto-generated method stub
									BaseUtils.showToast(mReference.get(), list
											.get(arg2).toString());

								}
							});
					break;

				case JSON_REC_PARSE_FAIL:
					Log.i(MYORDER_HANDLER_TAG, "json parse failed");
					mDialog.dismiss();
					BaseUtils.showToast(mReference.get(), "解析数据失败!");
					break;

				default:
					break;
			}
		}
	}

}
